package com.ruoyi.user.service.impl;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.user.domain.UserTotalCapital;
import com.ruoyi.user.mapper.UserTotalCapitalMapper;
import com.ruoyi.user.service.IUserTotalCapitalService;


/**
 * 资金余额Service业务层处理
 * 
 * @author DEREK
 * @date 2023-05-22
 */
@Service
public class UserTotalCapitalServiceImpl extends ServiceImpl<UserTotalCapitalMapper, UserTotalCapital> implements IUserTotalCapitalService 
{
    @Autowired
    private UserTotalCapitalMapper userTotalCapitalMapper;

    /**
     * 查询资金余额
     * 
     * @param id 资金余额主键
     * @return 资金余额
     */
    @Override
    public UserTotalCapital selectUserTotalCapitalById(Long id)
    {
        return userTotalCapitalMapper.selectUserTotalCapitalById(id);
    }

    /**
     * 查询资金余额列表
     * 
     * @param userTotalCapital 资金余额
     * @return 资金余额
     */
    @Override
    public List<UserTotalCapital> selectUserTotalCapitalList(UserTotalCapital userTotalCapital)
    {
        return userTotalCapitalMapper.selectUserTotalCapitalList(userTotalCapital);
    }

    /**
     * 新增资金余额
     * 
     * @param userTotalCapital 资金余额
     * @return 结果
     */
    @Override
    public int insertUserTotalCapital(UserTotalCapital userTotalCapital)
    {
        userTotalCapital.setCreateTime(DateUtils.getNowDate());
        return userTotalCapitalMapper.insertUserTotalCapital(userTotalCapital);
    }

    /**
     * 修改资金余额
     * 
     * @param userTotalCapital 资金余额
     * @return 结果
     */
    @Override
    public int updateUserTotalCapital(UserTotalCapital userTotalCapital)
    {
        return userTotalCapitalMapper.updateUserTotalCapital(userTotalCapital);
    }

    /**
     * 批量删除资金余额
     * 
     * @param ids 需要删除的资金余额主键
     * @return 结果
     */
    @Override
    public int deleteUserTotalCapitalByIds(Long[] ids)
    {
        return userTotalCapitalMapper.deleteUserTotalCapitalByIds(ids);
    }

    /**
     * 删除资金余额信息
     * 
     * @param id 资金余额主键
     * @return 结果
     */
    @Override
    public int deleteUserTotalCapitalById(Long id)
    {
        return userTotalCapitalMapper.deleteUserTotalCapitalById(id);
    }
    
    
	@Override
	public UserTotalCapital initUserTotalCapital(String amountType, Long userId,BigDecimal total,Integer type) {
		UserTotalCapital userTotalCapital = this.getOne(new LambdaQueryWrapper<UserTotalCapital>().eq(UserTotalCapital::getAmountType, amountType).eq(UserTotalCapital::getUserId, userId));
		if(null == userTotalCapital) {
			userTotalCapital = new UserTotalCapital();
			userTotalCapital.setAmountType(amountType);
			userTotalCapital.setCreateTime(new Date());
			userTotalCapital.setUserId(userId);
			userTotalCapital.setTotalAmount(total);
			userTotalCapital.setFrozenAmount(BigDecimal.ZERO);
			userTotalCapital.setWaitAmount(BigDecimal.ZERO);
			userTotalCapital.setCanAmount(BigDecimal.ZERO);
			this.save(userTotalCapital);
		}else {
			if(1 == type) {
				userTotalCapital.setTotalAmount(userTotalCapital.getTotalAmount().add(total));
			}else {
				userTotalCapital.setTotalAmount(userTotalCapital.getTotalAmount().subtract(total));
				if(userTotalCapital.getTotalAmount().compareTo(BigDecimal.ZERO) <= 0) {
					userTotalCapital.setTotalAmount(BigDecimal.ZERO);
				}
			}
			this.updateById(userTotalCapital);
		}
		return userTotalCapital;
	}

	@Override
	public boolean checkUserAmount(String amountType, Long userId, BigDecimal total) {
		UserTotalCapital userTotalCapital = this.getOne(new LambdaQueryWrapper<UserTotalCapital>().eq(UserTotalCapital::getAmountType, amountType).eq(UserTotalCapital::getUserId, userId));
		if(null == userTotalCapital) {
			return false;
		}
		BigDecimal totalAmount = userTotalCapital.getTotalAmount().subtract(userTotalCapital.getFrozenAmount());
		if(totalAmount.compareTo(total) == -1) {
			return false;
		}
		return true;
	}
	public static void main(String[] args) {
		System.out.println(new BigDecimal("5000").compareTo(new BigDecimal("3443")));
		System.out.println(Arrays.asList("202".split(",")));
	}

	@Override
	public UserTotalCapital initData(String amountType, Long userId) {
		UserTotalCapital userTotalCapital = this.getOne(new LambdaQueryWrapper<UserTotalCapital>().eq(UserTotalCapital::getAmountType, amountType).eq(UserTotalCapital::getUserId, userId));
		if(null == userTotalCapital) {
			userTotalCapital = new UserTotalCapital();
			userTotalCapital.setAmountType(amountType);
			userTotalCapital.setCreateTime(new Date());
			userTotalCapital.setUserId(userId);
			userTotalCapital.setTotalAmount(BigDecimal.ZERO);
			userTotalCapital.setFrozenAmount(BigDecimal.ZERO);
			userTotalCapital.setWaitAmount(BigDecimal.ZERO);
			userTotalCapital.setCanAmount(BigDecimal.ZERO);
			this.save(userTotalCapital);
		}else {
			boolean flag = false;
			if(null == userTotalCapital.getTotalAmount()) {
				userTotalCapital.setTotalAmount(BigDecimal.ZERO);
				flag = true;
			}
			if(null == userTotalCapital.getFrozenAmount()) {
				userTotalCapital.setFrozenAmount(BigDecimal.ZERO);
				flag = true;
			}
			if(null == userTotalCapital.getWaitAmount()) {
				userTotalCapital.setWaitAmount(BigDecimal.ZERO);
				flag = true;
			}
			if(null == userTotalCapital.getCanAmount()) {
				userTotalCapital.setCanAmount(BigDecimal.ZERO);
				flag = true;
			}
			if(flag) {
				this.updateById(userTotalCapital);
			}
		}
		return userTotalCapital;
	}

	@Override
	public int settlementWithdrawalAmount() {
		return userTotalCapitalMapper.settlementWithdrawalAmount();
	}
}
